package com.woniuxy.commons.exception;


import com.woniuxy.commons.utile.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

//异常处理类
@RestControllerAdvice
@Slf4j
public class ProjectExceptionHandler {

    //短信登录的自定义异常
    @ExceptionHandler(LoginPhoneException.class)
    public Result LoginPhoneException(LoginPhoneException e){
        log.error("出现异常",e);
        return Result.instance().setCode(e.getCode()).setMessage(e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result accessDeniedException(AccessDeniedException e){
        log.error("出现异常",e);
        return Result.instance().setCode(402).setMessage(e.getMessage());
    }

    /**
     * 注册的自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(RegisterException.class)
    public Result registerException(RegisterException e){
        log.error("出现异常",e);
        return Result.instance().setCode(1001).setMessage(e.getMessage());
    }

    /**
     * 服务指南的自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(GuidanceException.class)
    public Result guidanceException(GuidanceException e){
        log.error("出现异常",e);
        return Result.instance().setCode(1001).setMessage(e.getMessage());
    }

    /**
     * 通知公告的自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(NoticeException.class)
    public Result noticeException(NoticeException e){
        log.error("出现异常",e);
        return Result.instance().setCode(1001).setMessage(e.getMessage());
    }

    /**
     * 账号的自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccountException.class)
    public Result accountException(AccountException e){
        log.error("出现异常",e);
        return Result.instance().setCode(1001).setMessage(e.getMessage());
    }

    /**
     * 通知公告的自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(MaterialException.class)
    public Result materialException(MaterialException e){
        log.error("出现异常",e);
        return Result.instance().setCode(1001).setMessage(e.getMessage());
    }

    //兜底
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        log.error("出现异常",e);
        return Result.instance().setCode(500).setMessage(e.getMessage());
    }
}
